package com.cyz.admin.controller;

import com.cyz.common.dto.StudyResourceDto;
import com.cyz.common.dto.StudyResourceTypeDto;
import com.cyz.admin.service.StudyResourceService;
import com.cyz.admin.service.StudyResourceTypeService;
import com.cyz.common.utils.R;
import com.cyz.common.validated.AddGroup;
import com.cyz.common.validated.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "管理端学习资源类接口")
@RestController
@RequestMapping("/studyResource")
public class StudyResourceController {

    //TODO:权限

    @Autowired
    private StudyResourceTypeService studyResourceTypeService;
    @Autowired
    private StudyResourceService studyResourceService;

    @PreAuthorize("@ps.hasPermission('contant:study:resource')")
    @ApiOperation(value = "查询学习资源")
    @GetMapping("/queryStudyResources")
    public R queryStudyResources(){
        return studyResourceService.queryStudyResources();
    }

    @PreAuthorize("@ps.hasPermission('contant:study:resource')")
    @ApiOperation(value = "查询学习资源及其分类")
    @GetMapping("/queryStudyResourcesAndTypes")
    public R queryStudyResourcesAndTypes(){
        return studyResourceService.queryStudyResourcesAndTypes();
    }

    @PreAuthorize("@ps.hasPermission('contant:study:resource')")
    @ApiOperation(value = "更新学习资源")
    @PutMapping("/updateStudyResource")
    public R updateStudyResource(@Validated(UpdateGroup.class) @RequestBody StudyResourceDto studyResourceDto){
        return studyResourceService.updateStudyResource(studyResourceDto);
    }

    @PreAuthorize("@ps.hasPermission('contant:study:resource')")
    @ApiOperation(value = "增加学习资源")
    @PostMapping("/addStudyResource")
    public R addStudyResource(@Validated(AddGroup.class) @RequestBody StudyResourceDto studyResourceDto){
        return studyResourceService.addStudyResource(studyResourceDto);
    }

    @PreAuthorize("@ps.hasPermission('contant:study:resource')")
    @ApiOperation(value = "删除学习资源")
    @DeleteMapping("/deleteStudyResources")
    public R deleteStudyResources(Integer[] ids){
        return studyResourceService.deleteStudyResources(ids);
    }


    @PreAuthorize("@ps.hasPermission('contant:study:button')")
    @ApiOperation(value = "查询学习资源分类")
    @GetMapping("/queryStudyResourceTypes")
    public R queryStudyResourceTypes(){
        return studyResourceTypeService.queryStudyResourceTypes();
    }

    @PreAuthorize("@ps.hasPermission('contant:study:button')")
    @ApiOperation(value = "更新学习资源分类")
    @PutMapping("/updateStudyResourceType")
    public R updateStudyResourceType(@RequestBody StudyResourceTypeDto studyResourceTypeDto){
        return studyResourceTypeService.updateStudyResourceType(studyResourceTypeDto);
    }

    @PreAuthorize("@ps.hasPermission('contant:study:button')")
    @ApiOperation(value = "增加学习资源分类")
    @PostMapping("/addStudyResourceType")
    public R addStudyResourceType(@Validated(AddGroup.class) @RequestBody StudyResourceTypeDto studyResourceTypeDto){
        return studyResourceTypeService.addStudyResourceType(studyResourceTypeDto);
    }

    @PreAuthorize("@ps.hasPermission('contant:study:button')")
    @ApiOperation(value = "删除学习资源分类")
    @DeleteMapping("/deleteStudyResourceTypes")
    public R deleteStudyResourceTypes(Integer[] ids){
        return studyResourceTypeService.deleteStudyResourceTypes(ids);
    }
}
